package com.xja.listener;

import com.xja.utils.EmailUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author 蔡建国
 * Date  2025/5/30 20:40
 */
@Component
@Slf4j
public class EmailListener {

    @Autowired
    private EmailUtil emailUtil;

    // 利用RabbitListener来声明要监听的队列信息
    // 将来一旦监听的队列中有了消息，就会推送给当前服务，调用当前方法，处理消息。
    // 可以看到方法体中接收的就是消息体的内容
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "email.queue", durable = "true"),
            exchange = @Exchange(name = "email.topic"),
            key = "success"
    ))
    public void listenSimpleQueueMessage(Map<String, String> map) throws Exception{
        log.info("邮件消费者收到消息：{}", map);
        String email = map.get("email");
        String code = map.get("code");
        emailUtil.sendVerificationCode(email, code);
        log.info("邮件发送成功！");
    }
}